package com.fau.wm.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Hibernate;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.fau.wm.entity.MedicalAdmin;
import com.fau.wm.entity.MedicalAllocation;
import com.fau.wm.entity.MedicalAllocationDetail;
import com.fau.wm.entity.MedicalInStock;
import com.fau.wm.entity.MedicalInventory;
import com.fau.wm.entity.MedicalInventoryDetail;
import com.fau.wm.entity.MedicalKeyValue;
import com.fau.wm.entity.MedicalMedicine;
import com.fau.wm.entity.MedicalRecallDetail;
import com.fau.wm.entity.MedicalStockoutWarn;
import com.fau.wm.entity.MedicalStorageDetail;
import com.fau.wm.entity.MedicalWarehouse;
import com.fau.wm.service.IInStockManagerService;
import com.fau.wm.util.Page;
import com.fau.wm.util.ParamUtil;
import com.fau.wm.util.SystemConfig;

@Service
@Transactional(readOnly = true)
public class InStockManagerService extends BaseService implements
		IInStockManagerService {

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void saveInStock(List<MedicalStorageDetail> storageDetailList) {
		for(MedicalStorageDetail storageDetail : storageDetailList){
			MedicalStorageDetail storageDetail1 = (MedicalStorageDetail) this.iBaseDao.getObject(MedicalStorageDetail.class, storageDetail.getId());
			Hibernate.initialize(storageDetail1.getStorage());
			MedicalInStock inStock = (MedicalInStock) this.iBaseDao.getObjectByHql("from MedicalInStock a where 1=1 and a.warehouse.id=? and a.medicine.id=? ", storageDetail1.getStorage().getWarehouse().getId(), storageDetail1.getMedicine().getId());
			if(null == inStock){
				MedicalInStock bean = new MedicalInStock();
				MedicalMedicine medicine = (MedicalMedicine) this.iBaseDao.getObject(MedicalMedicine.class, storageDetail1.getMedicine().getId());
				bean.setMedicine(medicine);
				MedicalWarehouse warehouse = (MedicalWarehouse) this.iBaseDao.getObject(MedicalWarehouse.class, storageDetail1.getStorage().getWarehouse().getId());
				bean.setWarehouse(warehouse);
				bean.setQuantity(storageDetail.getQuantity());
				bean.setHospitalId(medicine.getHospitalId());
				this.iBaseDao.saveObject(bean);
			}else{
				inStock.setQuantity(inStock.getQuantity() + storageDetail.getQuantity());
			}
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public Page findInStockByPage(Map<String, Object> map,MedicalInStock inStock) {
		StringBuilder statusSql = new StringBuilder("from MedicalInStock a where a.hospitalId = ?");
		StringBuilder countSql = new StringBuilder("select count(*) from MedicalInStock a where a.hospitalId = ?");
		
		List<Object> objectList = new ArrayList<Object>();
		objectList.add(inStock.getHospitalId());
		
		if(StringUtils.isNotBlank(inStock.getMedicineName())){
			countSql.append("and a.medicine.name like ? ");
			statusSql.append("and a.medicine.name like ? ");
			objectList.add("%"+inStock.getMedicineName()+"%");
		}
		if(StringUtils.isNotBlank(inStock.getWarehouseName())){
			countSql.append("and a.warehouse.name like ? ");
			statusSql.append("and a.warehouse.name like ? ");
			objectList.add("%"+inStock.getWarehouseName()+"%");
		}
		if(StringUtils.isNotBlank(inStock.getMedicineType())){
			countSql.append("and a.medicine.medicineType = ? ");
			statusSql.append("and a.medicine.medicineType = ? ");
			objectList.add(inStock.getMedicineType());
		}
		
		Page page = this.iBaseDao.getPage(ParamUtil.getInteger(map,"pageIndex",1),ParamUtil.getInteger(map, "pageSize", 20),
				map, countSql.toString(), statusSql.toString(), objectList);
		List<MedicalInStock> list = (List<MedicalInStock>) page.getResult();
		MedicalAdmin user = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		for (MedicalInStock medicalInStock : list) {
			MedicalKeyValue kv = this.iBaseDao.getValueByKey(user.getEnterpriseId(), medicalInStock.getMedicine().getUnit());
			medicalInStock.getMedicine().setUnitValue(kv.getKeyValue());
			kv = this.iBaseDao.getValueByKey(user.getEnterpriseId(), medicalInStock.getMedicine().getMedicineType());
			medicalInStock.getMedicine().setMedicineTypeValue(kv.getKeyValue());
		}
		return page;
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void recallMinusInStock(List<MedicalRecallDetail> recallDetailList) {
		for(MedicalRecallDetail recallDetail : recallDetailList){
			MedicalRecallDetail bean = (MedicalRecallDetail) this.iBaseDao.getObject(MedicalRecallDetail.class, recallDetail.getId());
			Hibernate.isInitialized(bean.getRecall());
			MedicalInStock inStock = (MedicalInStock) this.iBaseDao.getObjectByHql("from MedicalInStock a where a.warehouse.id=? and a.medicine.id=? ", bean.getRecall().getWarehouse().getId(), recallDetail.getMedicine().getId());
			inStock.setQuantity(inStock.getQuantity() - recallDetail.getQuantity());
		}
	}

	@Override
	public Boolean checkMedicineInStock(String medicineId, String warehouseId, Integer quantity) {
		MedicalInStock inStock = (MedicalInStock) this.iBaseDao.getObjectByHql("from MedicalInStock a where a.medicine.id=? and a.warehouse.id=? ", medicineId, warehouseId);
		if(null == inStock || inStock.getQuantity() < quantity){
			return false;
		}else{
			return true;
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void inventoryUpdateInStock(
			List<MedicalInventoryDetail> inventoryDetailList) {
		for(MedicalInventoryDetail inventoryDetail : inventoryDetailList){
			MedicalInventory inventory = (MedicalInventory) this.iBaseDao.getObject(MedicalInventory.class, inventoryDetail.getInventory().getId());
			MedicalInStock inStock = (MedicalInStock) this.iBaseDao.getObjectByHql("from MedicalInStock a where 1=1 and a.warehouse.id=? and a.medicine.id=? ", inventory.getWarehouse().getId(), inventoryDetail.getMedicine().getId());
			if(null == inStock){
				MedicalInStock bean = new MedicalInStock();
				MedicalMedicine medicine = (MedicalMedicine) this.iBaseDao.getObject(MedicalMedicine.class, inventoryDetail.getMedicine().getId());
				bean.setMedicine(medicine);
				MedicalWarehouse warehouse = (MedicalWarehouse) this.iBaseDao.getObject(MedicalWarehouse.class, inventory.getWarehouse().getId());
				bean.setWarehouse(warehouse);
				bean.setQuantity(inventoryDetail.getQuantity());
				this.iBaseDao.saveObject(bean);
			} else {
				inStock.setQuantity(inStock.getQuantity() + inventoryDetail.getQuantity());
			}
		}
		
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void saveAllocation(MedicalAllocation allocation, MedicalAdmin writeUser) {
		MedicalWarehouse recallWarehouse = (MedicalWarehouse) this.iBaseDao.getObject(MedicalWarehouse.class, allocation.getRecallWhId());
		allocation.setRecallWarehouse(recallWarehouse);
		MedicalWarehouse callinWarehouse = (MedicalWarehouse) this.iBaseDao.getObject(MedicalWarehouse.class, allocation.getCallinWhId());
		allocation.setCallinWarehouse(callinWarehouse);
		allocation.setWriteUser(writeUser);
		allocation.setHospitalId(writeUser.getEnterpriseId());
		this.iBaseDao.saveObject(allocation);
	}
	
	@Override
	public Boolean verSerialNum(String serialNum,String hospitalId){
		MedicalAllocation allocation = (MedicalAllocation)this.iBaseDao.getObjectByHql("from MedicalAllocation a where a.serialNum=? and a.hospitalId=? ", serialNum,hospitalId);
		if(null == allocation){
			return true;
		}else{
			return false;
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void saveEditAllocation(MedicalAllocation allocation) {
		MedicalAllocation bean = (MedicalAllocation) this.iBaseDao.getObject(MedicalAllocation.class, allocation.getId());
		MedicalWarehouse recallWarehouse = (MedicalWarehouse) this.iBaseDao.getObject(MedicalWarehouse.class, allocation.getRecallWhId());
		bean.setRecallWarehouse(recallWarehouse);
		MedicalWarehouse callinWarehouse = (MedicalWarehouse) this.iBaseDao.getObject(MedicalWarehouse.class, allocation.getCallinWhId());
		bean.setCallinWarehouse(callinWarehouse);
		bean.setSerialNum(allocation.getSerialNum());
		bean.setTitle(allocation.getTitle());
		bean.setAllocationTime(allocation.getAllocationTime());
		bean.setUpdateTime(new Date());
		this.iBaseDao.saveObject(bean);
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void delAllocation(MedicalAllocation allocation) {
		String[] ids = allocation.getId().split("-");
		for (String id : ids) {
			this.iBaseDao.updateHql("delete from MedicalAllocationDetail a where a.allocation.id=? ", id);
			this.iBaseDao.updateHql("delete from MedicalAllocation a where a.id=? ", id);
		}
	}

	@Override
	public Page findAllocationByPage(Map<String, Object> map,MedicalAllocation allocation) {
		StringBuilder statusSql = new StringBuilder("from MedicalAllocation a where a.hospitalId=? and a.status in(?,?,?) ");
		StringBuilder countSql = new StringBuilder("select count(*) from MedicalAllocation a where a.hospitalId=? and a.status in(?,?,?) ");
		List<Object> objectList = new ArrayList<Object>();
		objectList.add(allocation.getHospitalId());
		objectList.add(SystemConfig.ALLOCATION_STATUS_APPROVAL);
		objectList.add(SystemConfig.ALLOCATION_STATUS_REJECT);
		objectList.add(SystemConfig.ALLOCATION_STATUS_NEW);
		
		if(StringUtils.isNotBlank(allocation.getTitle())){
			countSql.append("and a.title like ? ");
			statusSql.append("and a.title like ? ");
			objectList.add("%"+allocation.getTitle()+"%");
		}
		
		if(StringUtils.isNotBlank(allocation.getSerialNum())){
			countSql.append("and a.serialNum = ? ");
			statusSql.append("and a.serialNum = ? ");
			objectList.add(allocation.getSerialNum());
		}
		
		if(StringUtils.isNotBlank(allocation.getRecallWarehouseName())){
			countSql.append("and a.recallWarehouse.name like ? ");
			statusSql.append("and a.recallWarehouse.name like ? ");
			objectList.add("%"+allocation.getRecallWarehouseName()+"%");
		}
		
		if(StringUtils.isNotBlank(allocation.getCallinWarehouseName())){
			countSql.append("and a.callinWarehouse.name like ? ");
			statusSql.append("and a.callinWarehouse.name like ? ");
			objectList.add("%"+allocation.getCallinWarehouseName()+"%");
		}
		
		statusSql.append("order by a.createTime desc ");
		
		Page page = this.iBaseDao.getPage(ParamUtil.getInteger(map,"pageIndex",1),ParamUtil.getInteger(map, "pageSize", 20),
				map, countSql.toString(), statusSql.toString(), objectList);
		
		return page;
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void saveAllocationDetail(MedicalAllocationDetail allocationDetail) {
		MedicalMedicine medicine = (MedicalMedicine) this.iBaseDao.getObject(MedicalMedicine.class, allocationDetail.getMedicineId());
		allocationDetail.setMedicine(medicine);
		MedicalAllocation allocation = (MedicalAllocation) this.iBaseDao.getObject(MedicalAllocation.class, allocationDetail.getAllocationId());
		allocationDetail.setAllocation(allocation);
		this.iBaseDao.saveObject(allocationDetail);
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void saveEditAllocationDetail(MedicalAllocationDetail allocationDetail) {
		MedicalAllocationDetail bean = (MedicalAllocationDetail) this.iBaseDao.getObject(MedicalAllocationDetail.class, allocationDetail.getId());
		MedicalMedicine medicine = (MedicalMedicine) this.iBaseDao.getObject(MedicalMedicine.class, allocationDetail.getMedicineId());
		bean.setMedicine(medicine);
		bean.setQuantity(allocationDetail.getQuantity());
		bean.setUpdateTime(new Date());
		this.iBaseDao.saveObject(bean);
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void delAllocationDetail(MedicalAllocationDetail allocationDetail) {
		this.iBaseDao.deleteObject(allocationDetail);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MedicalWarehouse> findWarehouseList() {
		MedicalAdmin user = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		return this.iBaseDao.getBySql("from MedicalWarehouse a where a.hospitalId=? ", null,user.getEnterpriseId());
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MedicalAllocationDetail> findallocationDetailList(String allocationId) {
		List<MedicalAllocationDetail> list = this.iBaseDao.getBySql("from MedicalAllocationDetail a where a.allocation.id=? ", null, allocationId);
		MedicalAdmin user = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		for (MedicalAllocationDetail medicalAllocationDetail : list) {
			MedicalKeyValue kv = this.iBaseDao.getValueByKey(user.getEnterpriseId(), medicalAllocationDetail.getMedicine().getUnit());
			medicalAllocationDetail.getMedicine().setUnitValue(kv.getKeyValue());
		}
		return list;
	}

	@Override
	public Boolean checkMedicineExist(String medicineId, String allocationId) {
		MedicalAllocationDetail allocationDetail = (MedicalAllocationDetail) this.iBaseDao.getObjectByHql("from MedicalAllocationDetail a where a.medicine.id=? and a.allocation.id=? ", medicineId, allocationId);
		if(null == allocationDetail){
			return true;
		}else {
			return false;
		}
	}

	@Override
	public Page findAllocationAuditByPage(Map<String, Object> map,
			MedicalAllocation allocation) {
		StringBuilder statusSql = new StringBuilder("from MedicalAllocation a where a.hospitalId=? ");
		StringBuilder countSql = new StringBuilder("select count(*) from MedicalAllocation a where a.hospitalId=? ");
		
		List<Object> objectList = new ArrayList<Object>();
		objectList.add(allocation.getHospitalId());
		
		if(StringUtils.isNotBlank(allocation.getTitle())){
			countSql.append("and a.title like ? ");
			statusSql.append("and a.title like ? ");
			objectList.add("%"+allocation.getTitle()+"%");
		}
		
		if(StringUtils.isNotBlank(allocation.getSerialNum())){
			countSql.append("and a.serialNum like ? ");
			statusSql.append("and a.serialNum like ? ");
			objectList.add("%"+allocation.getSerialNum()+"%");
		}
		
		if(StringUtils.isNotBlank(allocation.getRecallWarehouseName())){
			countSql.append("and a.recallWarehouse.name like ? ");
			statusSql.append("and a.recallWarehouse.name like ? ");
			objectList.add("%"+allocation.getRecallWarehouseName()+"%");
		}
		
		if(StringUtils.isNotBlank(allocation.getCallinWarehouseName())){
			countSql.append("and a.callinWarehouse.name like ? ");
			statusSql.append("and a.callinWarehouse.name like ? ");
			objectList.add("%"+allocation.getCallinWarehouseName()+"%");
		}
		
		statusSql.append("and a.status=? ");
		countSql.append("and a.status=? ");
		objectList.add(SystemConfig.ALLOCATION_STATUS_PENDING);
		
		statusSql.append("order by a.createTime desc ");
		
		Page page = this.iBaseDao.getPage(ParamUtil.getInteger(map,"pageIndex",1),ParamUtil.getInteger(map, "pageSize", 20),
				map, countSql.toString(), statusSql.toString(), objectList);
		
		return page;
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void updateAllocationStatus(String status, String id) {
		MedicalAllocation allocation = (MedicalAllocation) this.iBaseDao.getObject(MedicalAllocation.class, id);
		allocation.setStatus(status);
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void updateAuditUser(MedicalAdmin auditUser, String id) {
		MedicalAllocation allocation = (MedicalAllocation) this.iBaseDao.getObject(MedicalAllocation.class, id);
		allocation.setAuditUser(auditUser);
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void allocation(List<MedicalAllocationDetail> allocationDetailList, String allocationId) {
		MedicalAllocation allocation = (MedicalAllocation) this.iBaseDao.getObject(MedicalAllocation.class, allocationId);
		for(MedicalAllocationDetail allocationDetail : allocationDetailList){
			//调出库，库存
			MedicalInStock recallInStock = (MedicalInStock) this.iBaseDao.getObjectByHql("from MedicalInStock a where a.medicine.id=? and a.warehouse.id=? ", allocationDetail.getMedicine().getId(), allocation.getRecallWarehouse().getId());
			recallInStock.setQuantity(recallInStock.getQuantity() - allocationDetail.getQuantity());
			//调入库，库存
			MedicalInStock callinInStock = (MedicalInStock) this.iBaseDao.getObjectByHql("from MedicalInStock a where a.medicine.id=? and a.warehouse.id=? ", allocationDetail.getMedicine().getId(), allocation.getCallinWarehouse().getId());
			if(null != callinInStock) {
				callinInStock.setQuantity(callinInStock.getQuantity() + allocationDetail.getQuantity());
			}else {
				MedicalInStock inStock = new MedicalInStock();
				MedicalMedicine medicine = (MedicalMedicine) this.iBaseDao.getObject(MedicalMedicine.class, allocationDetail.getMedicine().getId());
				inStock.setMedicine(medicine);
				MedicalWarehouse warehouse = (MedicalWarehouse) this.iBaseDao.getObject(MedicalWarehouse.class, allocation.getCallinWarehouse().getId());
				inStock.setWarehouse(warehouse);
				inStock.setQuantity(allocationDetail.getQuantity());
				inStock.setHospitalId(warehouse.getHospitalId());
				this.iBaseDao.saveObject(inStock);
			}
		}
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<MedicalInStock> findMedicineUnderWarn() {
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		List<MedicalInStock> mlist = new LinkedList<MedicalInStock>();
		// 获得所有的药品库存
		List<MedicalInStock> list = this.iBaseDao.getBySql("from MedicalInStock a where a.hospitalId=? order by a.medicine.name ", null, admin.getEnterpriseId());
		// 全局提醒数
		MedicalStockoutWarn stockoutWarn = (MedicalStockoutWarn) this.iBaseDao.getObjectByHql("from MedicalStockoutWarn a where a.eid=? ", admin.getEnterpriseId());
		
		for (MedicalInStock inStock : list) {
			MedicalKeyValue keyValue = this.iBaseDao.getValueByKey(admin.getEnterpriseId(), inStock.getMedicine().getUnit());
			if(null != keyValue){
				inStock.getMedicine().setUnitValue(keyValue.getKeyValue());
			}
			if(null == inStock.getMedicine().getWarnNum()){
				if(null != stockoutWarn && inStock.getQuantity() <= stockoutWarn.getNum()){
					mlist.add(inStock);
				}
			} else {
				if(inStock.getQuantity() <= inStock.getMedicine().getWarnNum()){
					mlist.add(inStock);
				}
			}
		}
		
		return mlist;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Page findAllocationOut(Map<String, Object> map, String medicineId,String warehouseId) {
		MedicalAdmin user = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		StringBuilder countSql = new StringBuilder("select count(*) from MedicalAllocationDetail a where a.medicine.id=? and a.allocation.recallWarehouse.id=? and a.allocation.status=? ");
		StringBuilder statusSql = new StringBuilder("from MedicalAllocationDetail a where a.medicine.id=? and a.allocation.recallWarehouse.id=? and a.allocation.status=? ");
		
		List<Object> objectList = new ArrayList<Object>();
		objectList.add(medicineId);
		objectList.add(warehouseId);
		objectList.add(SystemConfig.ALLOCATION_STATUS_APPROVAL);
		
		statusSql.append("order by a.createTime desc ");
		
		Page page = this.iBaseDao.getPage(ParamUtil.getInteger(map,"pageIndex",1),ParamUtil.getInteger(map, "pageSize", 20),
				map, countSql.toString(), statusSql.toString(), objectList);
		
		List<MedicalAllocationDetail> list = (List<MedicalAllocationDetail>) page.getResult();
		for (MedicalAllocationDetail medicalAllocationDetail : list) {
			MedicalKeyValue kv = this.iBaseDao.getValueByKey(user.getEnterpriseId(), medicalAllocationDetail.getMedicine().getUnit());
			medicalAllocationDetail.getMedicine().setUnitValue(kv.getKeyValue());
		}
		
		return page;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public Page findAllocationIn(Map<String, Object> map,String medicineId,String warehouseId) {
		MedicalAdmin user = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		StringBuilder countSql = new StringBuilder("select count(*) from MedicalAllocationDetail a where a.medicine.id=? and a.allocation.callinWarehouse.id=? and a.allocation.status=? ");
		StringBuilder statusSql = new StringBuilder("from MedicalAllocationDetail a where a.medicine.id=? and a.allocation.callinWarehouse.id=? and a.allocation.status=? ");
		
		List<Object> objectList = new ArrayList<Object>();
		objectList.add(medicineId);
		objectList.add(warehouseId);
		objectList.add(SystemConfig.ALLOCATION_STATUS_APPROVAL);
		
		statusSql.append("order by a.createTime desc ");
		
		Page page = this.iBaseDao.getPage(ParamUtil.getInteger(map,"pageIndex",1),ParamUtil.getInteger(map, "pageSize", 20),
				map, countSql.toString(), statusSql.toString(), objectList);
		
		List<MedicalAllocationDetail> list = (List<MedicalAllocationDetail>) page.getResult();
		for (MedicalAllocationDetail medicalAllocationDetail : list) {
			MedicalKeyValue kv = this.iBaseDao.getValueByKey(user.getEnterpriseId(), medicalAllocationDetail.getMedicine().getUnit());
			medicalAllocationDetail.getMedicine().setUnitValue(kv.getKeyValue());
		}
		
		return page;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public boolean toAudit(String sid) {
		List<MedicalAllocationDetail> list = this.iBaseDao.getBySql("from MedicalAllocationDetail a where a.allocation.id=? ", null, sid);
		if(list.size() < 1){
			return false;
		}else{
			this.iBaseDao.updateHql("update MedicalAllocation a set a.status=? where a.id=? ", SystemConfig.ALLOCATION_STATUS_PENDING,sid);
			return true;
		}
	}

	@Override
	public String validProExist(MedicalAllocationDetail allocationDetail) {
		StringBuilder errorInfo = new StringBuilder("");
		if(allocationDetail.getId().indexOf(",") != -1) {
			String[] detailIds = allocationDetail.getId().split(",");
			for(String dId : detailIds) {
				MedicalAllocationDetail bean = (MedicalAllocationDetail) this.iBaseDao.getObject(MedicalAllocationDetail.class, dId);
				MedicalInStock inStock = (MedicalInStock) this.iBaseDao.getObjectByHql("from MedicalInStock a where a.warehouse.id=? and a.medicine.id=? ", allocationDetail.getWarehouseId(), bean.getMedicine().getId());
				if(null != inStock && bean.getQuantity() <= inStock.getQuantity()) {
					continue;
				} else {
					errorInfo.append(bean.getMedicine().getName()+"、");
				}
			}
			if(StringUtils.isNotBlank(errorInfo.toString())) {
				errorInfo.deleteCharAt(errorInfo.lastIndexOf("、"));
			} else {
				errorInfo.append("true");
			}
		} else {
			MedicalAllocationDetail bean = (MedicalAllocationDetail) this.iBaseDao.getObject(MedicalAllocationDetail.class, allocationDetail.getId());
			MedicalInStock inStock = (MedicalInStock) this.iBaseDao.getObjectByHql("from MedicalInStock a where a.warehouse.id=? and a.medicine.id=? ", allocationDetail.getWarehouseId(), bean.getMedicine().getId());
			if(null != inStock && bean.getQuantity() > inStock.getQuantity()) {
				errorInfo.append("true");
			} else {
				errorInfo.append(bean.getMedicine().getName());
			}
		}
		return errorInfo.toString();
	}
	
}